package com.lecture.java8.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.lecture.java8.excel.model.UserInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author luyn
 * @description
 * @Date 2020/9/24 15:07
 * @Version 1.0
 */
public class UserExcelListener extends AnalysisEventListener<Map<String, Object>> {

    /**
     * 批处理阈值
     */
    private static final int BATCH_COUNT = 2;
    List<UserInfo> list = new ArrayList<UserInfo>(BATCH_COUNT);

    @Override
    public void invoke(Map<String, Object> userInfo, AnalysisContext analysisContext) {
        System.out.println("解析到一条数据:{}" + JSON.toJSONString(userInfo));
        System.out.println(userInfo.get(0));
        /*list.add(userInfo.get("name").toString());
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }*/
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //saveData();
        System.out.println("所有数据解析完成！");
    }
    private void saveData(){
        //System.out.println(list.size() +"条数据，开始存储数据库！");
        System.out.println("保存成功");
    }
}
